package com.pvdnc.systemdebugger.userauth;

import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;

import com.pvdnc.systemdebugger.compacter.IAuth;
import com.pvdnc.systemdebugger.compacter.utils.AppContext;

public class Auth extends Binder implements IAuth {
    private static final String TAG="Auth";

    private static int LAST_AUTH_RESULT=0;

    @Override
    public int request(String operation) {
        int fromPid=Binder.getCallingPid();
        TripleResultDialog.ensureLooper();
        TripleResultDialog dialog=new TripleResultDialog(AppContext.getAppContext());
        dialog.setTitle("授权请求："+ProcessUtils.getCallingInfo(fromPid));
        dialog.setMessage(operation);
        return dialog.showDialog();
    }

    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
        switch (code){
            case CODE_REQUEST:{
                String operation=data.readString();
                int authResult=request(operation);
                reply.writeInt(authResult);
                Log.d(TAG,"auth finished with result:"+authResult);
                break;
            }
            default:{
                Log.e(TAG,"unknown transact code:"+code);
                return false;
            }
        }
        return true;
    }

    @Override
    public IBinder asBinder() {
        return this;
    }
}
